package com.cop.consultapp.activity;

import java.util.ArrayList;
import java.util.Iterator;

import com.cop.consultapp.MainActivity;
import com.cop.consultapp.activity.ConsultListActivity.ConsultListHandler;
import com.cop.consultapp.adapter.ConsultListAdapter;
import com.cop.consultapp.model.QuestionVO;
import com.cop.consultapp.thread.ConsultListThread;
import com.cop.consultapp.thread.ConsultViewThread;
import com.kdn.consaltapp.R;
import com.kdn.consaltapp.R.layout;
import com.kdn.consaltapp.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ConsultViewActivity extends Activity {
	
	private ConsultViewThread viewThread = null;
	private ConsultViewHandler viewHandler = null;
	private ConsultViewActivity activity = null;
	
	private Button	btnHome  = null;
	private Button	btnModify  = null;
	private Button	btnDelete  = null;
	private Button	btnList  = null;
	
	private int idx = 0;
	
	private TextView textTitle = null; 
	private TextView textName = null;
	private TextView textTelno = null;
	private TextView textContent = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consult_view);
		
		this.activity = this;
		setViews();
		
		this.btnHome.setOnClickListener(onBtnHomeClickListener);
		this.btnModify.setOnClickListener(onBtnModifyClickListener);
		this.btnDelete.setOnClickListener(onBtnDeleteClickListener);
		this.btnList.setOnClickListener(onBtnListClickListener);
		
		
		Intent intent = getIntent();		
		this.idx = Integer.parseInt(intent.getStringExtra("IDX"));		
		
		this.viewHandler = new ConsultViewHandler();		
		this.viewThread = new ConsultViewThread(viewHandler, idx);
		this.viewThread.start();
	}

	private OnClickListener onBtnHomeClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			//home은 두단계 위에 있음...어떻게 할 것인가..
			Intent intent=new Intent(getApplicationContext(), MainActivity.class);
			startActivity(intent);			
		}
	};
	
	private OnClickListener onBtnModifyClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			Intent intent=new Intent(getApplicationContext(), ConsultInsertModifyActivity.class);
			
			Bundle bundle=new Bundle();
			bundle.putString("IDX", idx+"");				
			intent.putExtras(bundle);
			
			startActivity(intent);
			
		}
	};
	
	private OnClickListener onBtnDeleteClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			//delete는 무엇을 호출?
			
		}
	};
	
	private OnClickListener onBtnListClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			
			finish();
			
		}
	};
	
	private void setViews(){
		this.btnList = (Button) findViewById(R.id.btnViewList);
		this.btnModify = (Button) findViewById(R.id.btnViewModify);
		this.btnDelete = (Button) findViewById(R.id.btnViewDelete);
		this.btnHome = (Button) findViewById(R.id.won_home_btn);
		
		this.textTitle = (TextView) findViewById(R.id.textViewTitle);
		this.textName = (TextView) findViewById(R.id.textViewName);
		this.textTelno = (TextView) findViewById(R.id.textViewTelno);
		this.textContent = (TextView) findViewById(R.id.textViewContent);
	}
	
	public class ConsultViewHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Log.d("mylog", "결과값 도착!!!!!!!!!! by Handler Message...");
			
			ArrayList<QuestionVO> consultList = (ArrayList<QuestionVO>) msg.obj;
			
			if(consultList==null) return;
			
			Iterator<QuestionVO> consultIter = consultList.iterator();
			QuestionVO questionVO = null;
			
			while(consultIter.hasNext())
			{	
				questionVO = consultIter.next();
				Log.d("mylog", questionVO.getTitle());			
				
			}
			
			if(questionVO!=null){
				textTitle.setText(questionVO.getTitle());
				textName.setText(questionVO.getName());
				textTelno.setText(questionVO.getTelno());
				textContent.setText(questionVO.getContent());
			}
			
			
		}
	}
}
